home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / comm.jar / content / editor / EdFieldSetProps.js < prev    next >
Encoding:
JavaScript  |  2002-04-09  |  5.9 KB  |  200 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Field Set Properties Dialog.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Neil Rashbrook.
  18.  * Portions created by the Initial Developer are Copyright (C) 2001
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s): Neil Rashbrook <neil@parkwaycc.co.uk>
  22.  *
  23.  * Alternatively, the contents of this file may be used under the terms of
  24.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  25.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26.  * in which case the provisions of the GPL or the LGPL are applicable instead
  27.  * of those above. If you wish to allow use of your version of this file only
  28.  * under the terms of either the GPL or the LGPL, and not to allow others to
  29.  * use your version of this file under the terms of the MPL, indicate your
  30.  * decision by deleting the provisions above and replace them with the notice
  31.  * and other provisions required by the GPL or the LGPL. If you do not delete
  32.  * the provisions above, a recipient may use your version of this file under
  33.  * the terms of any one of the MPL, the GPL or the LGPL.
  34.  *
  35.  * ***** END LICENSE BLOCK ***** */
  36.  
  37. var insertNew;
  38. var fieldsetElement;
  39. var newLegend;
  40. var legendElement;
  41.  
  42. // dialog initialization code
  43.  
  44. function Startup()
  45. {
  46.   if (!InitEditorShell())
  47.     return;
  48.  
  49.   gDialog.editText = document.getElementById("EditText");
  50.   gDialog.legendText = document.getElementById("LegendText");
  51.   gDialog.legendAlign = document.getElementById("LegendAlign");
  52.   gDialog.RemoveFieldSet = document.getElementById("RemoveFieldSet");
  53.  
  54.   // Get a single selected field set element
  55.   var tagName = "fieldset";
  56.   fieldsetElement = editorShell.GetSelectedElement(tagName);
  57.   if (!fieldsetElement)
  58.     fieldsetElement = editorShell.GetElementOrParentByTagName(tagName, editorShell.editorSelection.anchorNode);
  59.   if (!fieldsetElement)
  60.     fieldsetElement = editorShell.GetElementOrParentByTagName(tagName, editorShell.editorSelection.focusNode);
  61.  
  62.   if (fieldsetElement)
  63.     // We found an element and don't need to insert one
  64.     insertNew = false;
  65.   else
  66.   {
  67.     insertNew = true;
  68.  
  69.     // We don't have an element selected,
  70.     //  so create one with default attributes
  71.  
  72.     fieldsetElement = editorShell.CreateElementWithDefaults(tagName);
  73.     if (!fieldsetElement)
  74.     {
  75.       dump("Failed to get selected element or create a new one!\n");
  76.       window.close();
  77.       return;
  78.     }
  79.     // Hide button removing existing fieldset
  80.     gDialog.RemoveFieldSet.setAttribute("hidden", "true");
  81.   }
  82.  
  83.   legendElement = fieldsetElement.firstChild;
  84.   if (legendElement && legendElement.localName == "LEGEND")
  85.   {
  86.     newLegend = false;
  87.     editorShell.SelectElement(legendElement);
  88.     gDialog.legendText.value = GetSelectionAsText();
  89.     if (/</.test(legendElement.innerHTML))
  90.     {
  91.       gDialog.editText.checked = false;
  92.       gDialog.editText.disabled = false;
  93.       gDialog.legendText.disabled = true;
  94.       gDialog.editText.addEventListener("command", onEditText, false);
  95.       gDialog.RemoveFieldSet.focus();
  96.     }
  97.     else
  98.       SetTextboxFocus(gDialog.legendText);
  99.   }
  100.   else
  101.   {
  102.     newLegend = true;
  103.  
  104.     // We don't have an element selected,
  105.     //  so create one with default attributes
  106.  
  107.     legendElement = editorShell.CreateElementWithDefaults("legend");
  108.     if (!legendElement)
  109.     {
  110.       dump("Failed to get selected element or create a new one!\n");
  111.       window.close();
  112.       return;
  113.     }
  114.     SetTextboxFocus(gDialog.legendText);
  115.   }
  116.  
  117.   // Make a copy to use for AdvancedEdit
  118.   globalElement = legendElement.cloneNode(false);
  119.  
  120.   InitDialog();
  121.  
  122.   SetWindowLocation();
  123. }
  124.  
  125. function InitDialog()
  126. {
  127.   gDialog.legendAlign.value = GetHTMLOrCSSStyleValue(globalElement, "align", "caption-side");
  128. }
  129.  
  130. function onEditText()
  131. {
  132.   gDialog.editText.removeEventListener("command", onEditText, false);
  133.   AlertWithTitle(GetString("Alert"), GetString("EditTextWarning"));
  134. }
  135.  
  136. function RemoveFieldSet()
  137. {
  138.   editorShell.BeginBatchChanges();
  139.   try {
  140.     if (!newLegend)
  141.       editorShell.DeleteElement(legendElement);
  142.     // This really needs to call the C++ function RemoveBlockContainer
  143.     // which inserts any <BR>s needed
  144.     RemoveElementKeepingChildren(fieldsetElement);
  145.   } finally {
  146.     editorShell.EndBatchChanges();
  147.   }
  148.   SaveWindowLocation();
  149.   window.close();
  150. }
  151.  
  152. function ValidateData()
  153. {
  154.   if (gDialog.legendAlign.value)
  155.     globalElement.setAttribute("align", gDialog.legendAlign.value);
  156.   else
  157.     globalElement.removeAttribute("align");
  158.   return true;
  159. }
  160.  
  161. function onAccept()
  162. {
  163.   // All values are valid - copy to actual element in doc
  164.   ValidateData();
  165.  
  166.   editorShell.BeginBatchChanges();
  167.  
  168.   try {
  169.     if (gDialog.editText.checked)
  170.     {
  171.       if (gDialog.legendText.value)
  172.       {
  173.         var editor = editorShell.editor;
  174.         if (newLegend)
  175.           editorShell.InsertElement(legendElement, fieldsetElement, 0, true);
  176.         else while (legendElement.firstChild)
  177.           editor.deleteNode(legendElement.firstChild);
  178.         editor.insertNode(editorShell.editorDocument.createTextNode(gDialog.legendText.value), legendElement, 0);
  179.       }
  180.       else if (!newLegend)
  181.         editorShell.DeleteElement(legendElement);
  182.     }
  183.  
  184.     if (insertNew)
  185.       InsertElementAroundSelection(fieldsetElement);
  186.     else
  187.       editorShell.SelectElement(fieldsetElement);
  188.  
  189.     editorShell.CloneAttributes(legendElement, globalElement);
  190.   }
  191.   finally {
  192.     editorShell.EndBatchChanges();
  193.   }
  194.  
  195.   SaveWindowLocation();
  196.  
  197.   return true;
  198. }
  199.  
  200.